﻿//Writen by HungNC on 03032009

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for CommonFunc
/// </summary>
public static class CommonFunc
{

    public static string DNSRoot()
    {
        string s = "";
        s = HttpContext.Current.Request.Url.ToString();
        string[] arr = s.Split('/');
        s = arr[0] + "//" + arr[2] + HttpContext.Current.Request.ApplicationPath;
        // s = HttpContext.Current.Request.ApplicationPath; 
        return s.Trim('/');
    }
    #region fck editor
   
    public static string FckEditorBasePath()
    {
        return DNSRoot() + "/fckeditor/";
    }

    public static string FckEditorSkinPath()
    {
        return FckEditorBasePath() + "editor/skins/office2003/";
    }
    #endregion

    public static void ShowAlertMessage(System.Web.UI.WebControls.Label ctl,string mess)
    {
        ctl.Text = mess;
        ctl.Visible = true;  
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="page"> trang hien tai</param>
    /// <param name="totalpage"> tong so trang</param>
    /// <param name="maxpage"> so trang xuat hien de chon </param>
    /// <returns></returns>
    public static string QueryPaging(int page, int totalpage, int maxpage)
    {

        string url = HttpContext.Current.Request.Url.ToString();

        string[] arrUrl = url.Split('?');

        if (arrUrl.Length > 1)
        {
            url = arrUrl[0] + "?";
            string[] arrQuery = arrUrl[1].Split('&');
            for (int i = 0; i < arrQuery.Length; i++)
            {
                if (arrQuery[i].IndexOf("p=") < 0)
                {
                    if (arrQuery[i].IndexOf("pageposition=") < 0)
                    {
                        url += arrQuery[i] + "&";
                    }
                }
            }
        }
        else
        {
            url = arrUrl[0] + "?";
        }

        int num_page = 0;
        int showpage = 0;
        int end = 0;
        int temppage = 0;
        int startpage = 0;
        string list_page = "";
        string list_page1 = "";
        string list_page2 = "";
        int back = 0;
        int next = 0;
        if (page >= maxpage)
        {
            //num_page = (int)Math.Ceiling((double)page / (double)maxpage);
            num_page = page / maxpage + 1;
            showpage = (num_page - 1) * maxpage;
            temppage = showpage;
            end = showpage + maxpage;
            showpage++;
        }
        else
        {
            temppage = 1;
            end = maxpage;
        }
        startpage = showpage;
        for (showpage = temppage; showpage < end + 1; showpage++)
        {
            if (showpage <= totalpage)
            {
                if (page == showpage)
                {

                    list_page += "<span class='PagingCurrent' >" + showpage.ToString() + "</span> ";
                }
                else
                {
                    list_page += "<a class='PagingNormal' href='" + url + "p=" + showpage.ToString() + "&pageposition=pageposition'>" + showpage.ToString() + "</a> ";
                }
            }
        }
        if (page > 1)
        {
            back = page - 1;
            list_page1 = "<a class='PagingNormal' title='Trang đầu tiên' href='" + url + "p=1&pageposition=pageposition'><img height='17' width='28' alt='thoi trang' src='../images/nav_previousset_on.gif'/></a> ";
            list_page1 += "<a class='PagingNormal'  title='Trang trước' href='" + url + "p=" + back.ToString() + "&pageposition=pageposition'>&nbsp;<img height='17' width='28' alt='thoi trang' src='../images/nav_previouspage_on.gif'/>&nbsp;</a> ";

        }

        if (page > maxpage)
        {
            list_page1 += "<a class='PagingNormal'  title='Trước' href='" + url + "p=" + Convert.ToString(page - maxpage) + "&pageposition=pageposition'>&nbsp;...&nbsp;</a>";
            //list_page1 += "<a  style='text-decoration:none;' title='Trang cuối cùng' href='" + url + "p=" + totalpage.ToString() + "&pageposition=pageposition'>&nbsp;" + totalpage.ToString() + "&nbsp;</a>";
        }

        if (totalpage - maxpage > maxpage + page)
        {
            list_page2 += "<a  class='PagingNormal'  title='Tiếp theo' href='" + url + "p=" + Convert.ToString(maxpage + page) + "&pageposition=pageposition'>&nbsp;...&nbsp;</a>";
            list_page2 += "<a class='PagingNormal'  title='Trang cuối cùng' href='" + url + "p=" + totalpage.ToString() + "&pageposition=pageposition'>&nbsp;" + totalpage.ToString() + "&nbsp;</a>";
        }

        if (page < totalpage)
        {
            next = page + 1;

            list_page2 += "<a class='PagingNormal'  title='Trang sau' href='" + url + "p=" + next + "&pageposition=pageposition'>&nbsp;<img height='17' width='28' alt='thoi trang' src='../images/nav_nextpage_on.gif'/>&nbsp;</a>";
            list_page2 += "<a class='PagingNormal'  title='Trang cuối cùng' href='" + url + "p=" + totalpage + "&pageposition=pageposition'><img height='17' width='28' alt='thoi trang' src='../images/nav_nextset_on.gif'/></a>";

        }

        list_page = " <a name='paggingPosition'></a>" + page.ToString() + "/" + totalpage.ToString() + " trang&nbsp;&nbsp;" + list_page1 + list_page + list_page2 + "&nbsp;&nbsp;";
        return list_page;
    }
   
    public static string QueryPaging(int page, int totalpage, int maxpage, string additionQuery)
    {

        string url = HttpContext.Current.Request.Url.ToString();

        string[] arrUrl = url.Split('?');

        if (arrUrl.Length > 1)
        {
            url = arrUrl[0] + "?";
            string[] arrQuery = arrUrl[1].Split('&');
            for (int i = 0; i < arrQuery.Length; i++)
            {
                if (arrQuery[i].IndexOf("p=") < 0)
                {
                    if (arrQuery[i].IndexOf("pageposition=") < 0)
                    {
                        url += arrQuery[i] + "&";
                    }
                }
            }
        }
        else
        {
            url = arrUrl[0] + "?";
        }

        if (additionQuery != "")
        {
            url += additionQuery+"&";
        }

        int num_page = 0;
        int showpage = 0;
        int end = 0;
        int temppage = 0;
        int startpage = 0;
        string list_page = "";
        string list_page1 = "";
        string list_page2 = "";
        int back = 0;
        int next = 0;
        if (page >= maxpage)
        {
            //num_page = (int)Math.Ceiling((double)page / (double)maxpage);
            num_page = page / maxpage + 1;
            showpage = (num_page - 1) * maxpage;
            temppage = showpage;
            end = showpage + maxpage;
            showpage++;
        }
        else
        {
            temppage = 1;
            end = maxpage;
        }
        startpage = showpage;
        for (showpage = temppage; showpage < end + 1; showpage++)
        {
            if (showpage <= totalpage)
            {
                if (page == showpage)
                {

                    list_page += "<span class='PagingCurrent' >" + showpage.ToString() + "</span> ";
                }
                else
                {
                    list_page += "<a class='PagingNormal' href='" + url + "p=" + showpage.ToString() + "&pageposition=pageposition'>" + showpage.ToString() + "</a> ";
                }
            }
        }
        if (page > 1)
        {
            back = page - 1;
            list_page1 = "<a class='PagingNormal' title='Trang đầu tiên' href='" + url + "p=1&pageposition=pageposition'><img height='17' width='28' alt='thoi trang' src='../images/nav_previousset_on.gif'/></a> ";
            list_page1 += "<a class='PagingNormal'  title='Trang trước' href='" + url + "p=" + back.ToString() + "&pageposition=pageposition'>&nbsp;<img height='17' width='28' alt='thoi trang' src='../images/nav_previouspage_on.gif'/>&nbsp;</a> ";

        }

        if (page > maxpage)
        {
            list_page1 += "<a class='PagingNormal'  title='Trước' href='" + url + "p=" + Convert.ToString(page - maxpage) + "&pageposition=pageposition'>&nbsp;...&nbsp;</a>";
            //list_page1 += "<a  style='text-decoration:none;' title='Trang cuối cùng' href='" + url + "p=" + totalpage.ToString() + "&pageposition=pageposition'>&nbsp;" + totalpage.ToString() + "&nbsp;</a>";
        }

        if (totalpage - maxpage > maxpage + page)
        {
            list_page2 += "<a  class='PagingNormal'  title='Tiếp theo' href='" + url + "p=" + Convert.ToString(maxpage + page) + "&pageposition=pageposition'>&nbsp;...&nbsp;</a>";
            list_page2 += "<a class='PagingNormal'  title='Trang cuối cùng' href='" + url + "p=" + totalpage.ToString() + "&pageposition=pageposition'>&nbsp;" + totalpage.ToString() + "&nbsp;</a>";
        }

        if (page < totalpage)
        {
            next = page + 1;

            list_page2 += "<a class='PagingNormal'  title='Trang sau' href='" + url + "p=" + next + "&pageposition=pageposition'>&nbsp;<img height='17' width='28' alt='thoi trang' src='../images/nav_nextpage_on.gif'/>&nbsp;</a>";
            list_page2 += "<a class='PagingNormal'  title='Trang cuối cùng' href='" + url + "p=" + totalpage + "&pageposition=pageposition'><img height='17' width='28' alt='thoi trang' src='../images/nav_nextset_on.gif'/></a>";

        }

        list_page = " <a name='paggingPosition'></a>" + page.ToString() + "/" + totalpage.ToString() + " trang&nbsp;&nbsp;" + list_page1 + list_page + list_page2 + "&nbsp;&nbsp;";
        return list_page;
    }
    
}
